package com.hq.fxh.No5_User.User.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.FollowUserModle;
import com.hq.fxh.Modle.MyResponseBody;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_GZ_People;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.data;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/15
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_GuanZhu_People extends Fragment {

    protected View rootView;
    protected PullToRefreshListView listGZPList;
    private Ladapter_GZ_People adapter;
    ArrayList<FollowUserModle.DataBean> data;
    int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_gz_people, null);
        initView(rootView);
        initData();
        refreshData();
        return rootView;
    }

    private void initView(View rootView) {
        listGZPList = (PullToRefreshListView) rootView.findViewById(R.id.list_GZP_List);
    }

    private void initData() {
        adapter = new Ladapter_GZ_People();
        listGZPList.setMode(PullToRefreshBase.Mode.BOTH);
        listGZPList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                refreshData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = page + 1;
                refreshData();

            }
        });

        adapter.setCallBack(new Ladapter_GZ_People.GZCallBack() {
            @Override
            public void call(TextView textView, int position, FollowUserModle.DataBean user) {
                int toUserID = user.getMember_id();
              addFollowerUser(2, toUserID,position);

                LogUtils.e("关注item="+position);
            }
        });

        listGZPList.setAdapter(adapter);
    }

    private void refreshData() {
        //     参数   1 我关注的用户  2我的粉丝 3 我关注的板块

        UserServiceImpl.GetFollowerList(1, page, 20, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                listGZPList.onRefreshComplete();

                LogUtils.e("关注的用户" + response.body().toString());
                LogUtils.e(response.body().toString());
                try {
                    ResponseBody body1 = response.body();
                    String s = body1.string();
                    FollowUserModle body = new Gson().fromJson(s, FollowUserModle.class);
                    List<FollowUserModle.DataBean> followData = body.getData();
                    if (data == null) {
                        data = new ArrayList<FollowUserModle.DataBean>();
                    }
                    if (page == 1) {
                        data.clear();
                    }
                    data.addAll(followData);
                    adapter.setData(data,2);
                    adapter.notifyDataSetChanged();
                } catch (Exception e) {
                    LogUtils.startToash(getContext(), "数据加载失败");
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                listGZPList.onRefreshComplete();
                LogUtils.startToash(getContext(), "数据加载失败");
                getActivity().finish();
            }
        });

    }


    private void addFollowerUser(int type, int to_uid, final int position) {
        if(to_uid== SharedPreferenceUtils.getMemberId(getContext())){
             LogUtils.startToash(getContext(), AppConstant.userself);
        }
        UserServiceImpl.addFollowerUser2(type, ""+to_uid, new Callback<MyResponseBody>() {
            @Override
            public void onResponse(Call<MyResponseBody> call, Response<MyResponseBody> response) {
                MyResponseBody body = response.body();
                if(body!=null){
                    try {
                    LogUtils.e(body.toString());

                    } catch (Exception e) {


                    }

                }
                if(data!=null&&data.size()>position){
                    data.remove(position);
                    adapter.setData(data,2);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(Call<MyResponseBody> call, Throwable t) {

                LogUtils.e("添加关注列表接口error  =  " + t.getMessage());

            }
        });

    }


}
